<?php

use Biz\model\brandActivity\BlindBoxPointTask as mBlindBoxPointTask;
use Biz\service\brand\BlindBox;
use Doraemon\service\Follow;
use Doraemon\tools\PointConfig;
use Hhzbeads\Point\PointClient;

/**
 * 关注品牌
 */
class FollowAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        try {
            $uid = $this->getRequest()->getPost('uid', 0);
            $action_from = $this->getRequest()->getPost('action_from', 'blind_box_follow');
            if (!$uid) {
                throw new \UnexpectedValueException('参数错误');
            }
            $key = $this->uid . '_brand_blindbox_follow_acttion';
            //5秒禁止提交
            $lock = \Biz\cache\brand\BrandShowActivity::isLock($key, 1);
            if ($lock) {
                throw new \UnexpectedValueException('请不要频繁提交');
            }

            $uids = explode(',', $uid);

            if ($uids){
                $act_from = 'blind_box';
                Follow::FollowMultiUsers($this->uid, $uids, $act_from);
            }

            //查询是否已经加过分
            $taskInfo = mBlindBoxPointTask::selectByWhere([
                'uid' => $this->uid,
                'obj_id' => $uids,
                'task_type' => mBlindBoxPointTask::TASK_TYPE_FOLLOW_BRAND
            ], ['obj_id']);
            $obj_ids = array_column($taskInfo, 'obj_id');
            //获取为加分的id
            $new_ids = array_diff($uids, $obj_ids);
//            $result = false;
            if ($new_ids) {
                $params['uid'] = $this->uid;
                $params['new_ids'] = $new_ids;
                $params['point'] = BlindBox::TASK_FOLLOW_BRAND_POINT;
                $params['event_name'] = PointConfig::POINT_TYPE_BLIND_BOX_FOLLOW_BRAND;
                $params['task_type'] = mBlindBoxPointTask::TASK_TYPE_FOLLOW_BRAND;
                $params['task_state'] = mBlindBoxPointTask::TASK_STATE_FINISHED;
                $result = BlindBox::addScore($params);
            }

//            $msg = '';
//            if ($result) {
                $msg = '关注成功';
//            }

        } catch (UnexpectedValueException $e) {
           return $this->setSuccess(['msg' => $e->getMessage()],0);
        } catch (Exception $e) {
            \Doraemon\tools\Log::error('品牌盲盒关注失败-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: '.$e->getMessage()],0);
        }
        \Doraemon\tools\ActionLog::log($this->uid, ['uid' => $this->uid, 'action_from' => $action_from], \Doraemon\tools\ActionLog::BLIND_BOX_FOLLOW);
        $this->setSuccess(['msg' => $msg]);
    }

}